package com.bala;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

import com.loveofsoftware.fotolab.EffectActivity;
import com.loveofsoftware.fotolab.PicfunActivity;
import com.loveofsoftware.fotolab.PostCameraActivity;
import com.loveofsoftware.fotolab.Util;

public class FotoLabActivity extends Activity {

	protected String fileFromCamera = "";
	
	protected int layoutId = R.layout.landing_screen;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		File outDir = new File(Util.getFotolabDir(this));
		if (!outDir.exists())
			outDir.mkdir();
		else if (outDir.isFile()) {
			Resources r = getResources();
			Toast.makeText(getApplicationContext(),
					r.getString(R.string.errFotolabIsAFile), Toast.LENGTH_LONG);
		}		
		setLandingScreen();
		delegateOnCreate();
	}
	
	protected void delegateOnCreate() {
		
	}
	

	protected void setLandingScreen() {
		final Activity activity = this;
		setContentView(layoutId);

		((ImageButton) findViewById(R.id.EffectImageButton))
				.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						Intent intent = new Intent(activity,
								EffectActivity.class);
						intent.putExtra("fileFromCamera", fileFromCamera);
						startActivity(intent);
					}

				});
		((ImageButton) findViewById(R.id.SketchImageButton))
				.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						Intent intent = new Intent(activity,
								PicfunActivity.class);
						intent.putExtra("fileFromCamera", fileFromCamera);
						startActivity(intent);
					}

				});
		((ImageButton) findViewById(R.id.CameraImageButton))
		.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
				startActivityForResult(intent, 0);
			}

		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// return super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.exit:
			this.finish();
			return true;
		case R.id.scanMedia:
			rescanMedia();
			return true;
		case R.id.camera:
			Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
			startActivityForResult(intent, 0);
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	public void rescanMedia() {
		sendBroadcast(new Intent(
				Intent.ACTION_MEDIA_MOUNTED,
				Uri.parse("file://" + Environment.getExternalStorageDirectory())));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0 && resultCode == Activity.RESULT_OK) {
			Bitmap x = (Bitmap) data.getExtras().get("data");
			ContentValues values = new ContentValues();
			values.put(Images.Media.TITLE, "fotolab");
			values.put(Images.Media.BUCKET_ID, "test");
			values.put(Images.Media.DESCRIPTION, "Image taken by fotolab");
			values.put(Images.Media.MIME_TYPE, "image/png");
			Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
					values);
			fileFromCamera = getPath(uri);
			OutputStream outstream;
			try {
				outstream = getContentResolver().openOutputStream(uri);

				x.compress(Bitmap.CompressFormat.PNG, 70, outstream);
				outstream.close();
				Toast.makeText(
						getApplicationContext(),
						"Select the effect. The picture you took will be automaticaly opened",
						Toast.LENGTH_LONG);
				
				Intent intent = new Intent(this, PostCameraActivity.class);
				intent.putExtra("fileUriFromCamera", uri);
				startActivity(intent);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	protected String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		if (cursor != null) {
			int column_index = cursor
					.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			cursor.moveToFirst();

			return cursor.getString(column_index);
		} else
			return null;
	}

}